#include "Normalizer.h"

#include <folly/logging/xlog.h>

namespace nexus {

Normalizer::Normalizer(Pipeline& pipeline) : pipeline_(pipeline) {}

void
Normalizer::run() {
  while (1) {
    std::shared_ptr<RouteUpdate> dpu;
    auto ticket = pipeline_.blockingReadStage<0>(dpu);
    process(*dpu);
    pipeline_.blockingWriteStage<0>(ticket, dpu);
  }
}

void
Normalizer::process(RouteUpdate& dpu) {
  std::for_each(
      std::execution::par,
      // std::execution::par_unseq,
      dpu.added.begin(),
      dpu.added.end(),
      [&](auto rule) {
        rule->hs = HeaderSpace::fromInt(rule->match, rule->prefixLength);
      });
}

} // namespace nexus